package com.drinks.yiniang.ui.widget.pop;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.FrameLayout;

import com.drinks.yiniang.R;
import com.drinks.yiniang.adapter.listview.CategoryPopAdapter;
import com.drinks.yiniang.entity.category.SubCategoryJson;
import com.hansen.library.listener.CheckDoubleClickListener;
import com.hansen.library.ui.widget.listview.FixedHeightListView;
import com.hansen.library.ui.widget.pop.YNPopupWindow;
import com.hansen.library.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author han
 * @date 2019/9/20
 */
public class CategoryPopup {

    //菜单显示PopupWindow
    private YNPopupWindow mCategoryPopupWindow;

    //分类adapter
    private CategoryPopAdapter mCategoryPopAdapter;

    private List<SubCategoryJson.SubCategoryList> mCategoryList;

    private AdapterView.OnItemClickListener onItemClickListener;

    public CategoryPopup(AdapterView.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;

        mCategoryList = new ArrayList<>();
    }

    public void showCategoryListPop(View anchor, Context context) {
        if (mCategoryPopupWindow == null) {
            //设置contentView
            View contentView = LayoutInflater.from(context).inflate(R.layout.pop_category_list, null);
            mCategoryPopupWindow = new YNPopupWindow(contentView,
                    ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
            mCategoryPopupWindow.setContentView(contentView);

            FrameLayout fl_category_pop_parent = contentView.findViewById(R.id.fl_category_pop_parent);

            FixedHeightListView lv_category_pop = contentView.findViewById(R.id.lv_category_pop);

            mCategoryPopAdapter = new CategoryPopAdapter(context, mCategoryList);

            lv_category_pop.setAdapter(mCategoryPopAdapter);
            lv_category_pop.setListViewHeightBasedOnChildren(10);
            lv_category_pop.setOnItemClickListener(onItemClickListener);

            //解决5.0以下版本点击外部不消失问题
            mCategoryPopupWindow.setOutsideTouchable(true);
            mCategoryPopupWindow.setBackgroundDrawable(new ColorDrawable());

            fl_category_pop_parent.setOnClickListener(new CheckDoubleClickListener() {
                @Override
                public void onCheckDoubleClick(View v) {
                    if(mCategoryPopupWindow != null) {
                        mCategoryPopupWindow.dismiss();
                    }
                }
            });
        }

        //显示方式
        mCategoryPopupWindow.showAsDropDown(anchor, 0, 2);
    }

    public void setCategorytList(List<SubCategoryJson.SubCategoryList> categoryList) {
        if(!CommonUtils.isEmptyList(categoryList)) {
            mCategoryList.addAll(categoryList);
        }
    }

    public List<SubCategoryJson.SubCategoryList> getCategoryList() {
        return mCategoryList;
    }

    public void dismissPop() {
        if(mCategoryPopupWindow != null && mCategoryPopupWindow.isShowing()) {
            mCategoryPopupWindow.dismiss();
        }
    }
}
